﻿''' <summary>
''' 更新タイムスタンプ列属性
''' </summary>
''' <remarks>
''' 追加時もタイムスタンプを記録します。
''' </remarks>
<AttributeUsage(AttributeTargets.Property)>
Public Class UpdateTimestampAttribute
    Inherits ColumnAttribute

    Shared ReadOnly COMMAND_TEXT As String = "CURRENT_TIMESTAMP"

#Region "Constructor"
    Public Sub New()
        Me.DirtyChecker = Function() False
        Me.IsLoadable = False
        Me.InsertColumnCommandCreater = AddressOf Me.CreateInsertColumnCommand
        Me.UpdateColumnCommandCreater = AddressOf Me.CraeteUpdateColumnCommand
    End Sub

    Public Sub New(ByVal ColumnName As String)
        Me.New()
        Me.ColumnName = ColumnName
    End Sub
#End Region

    Private _commandText As String
    Public Property CommandText As String
        Get
            Return If(_commandText Is Nothing, COMMAND_TEXT, _commandText)
        End Get
        Set(value As String)
            _commandText = value
        End Set
    End Property

    Private Sub CreateInsertColumnCommand(instance As Object, sqlColumnBuilder As System.Action(Of String), sqlValueBuilder As System.Action(Of String), paramRegister As System.Func(Of Object, Type, String))
        sqlColumnBuilder.Invoke(Me.ColumnName)
        sqlValueBuilder.Invoke(Me.CommandText)
    End Sub

    Private Sub CraeteUpdateColumnCommand(instance As Object, archiveValue As Object, sqlBuilder As Action(Of String), paramRegister As Func(Of Object, Type, String))
        sqlBuilder.Invoke(String.Format("{0} = {1}", Me.ColumnName, Me.CommandText))
    End Sub

End Class
